#include "configlogic.h"

void setTHdataToEeprom(THData data);
void config_set_state(u8 state);
void config_set_tempMin(u8 temp);
void config_set_tempMax(u8 temp);
void config_set_humiMin(u8 humi);
void config_set_humiMAx(u8 humi);
void config_set_sunshine(u8 light);
void config_set_smoke(u8 smoke);

u8 config_get_state(void);
u8 config_get_tempMin(void);
u8 config_get_tempMax(void);
u8 config_get_humiMin(void);
u8 config_get_humiMAx(void);
u8 config_get_sunshine(void);
u8 config_get_smoke(void);

int configTh(char* msg)
{
THData data;
 int res= parseTHData(&data,msg);
if (!res)return 0;
//把data数据设置给执行模块去使用
setTHdata(data);
//存储在本地的存储芯片中
setTHdataToEeprom(data);


return 1;
}
//写入数据到芯片
void setTHdataToEeprom(THData data)
{
//调用存储的函数把数据写入AT24C02芯片
	config_set_state(data.state);
	config_set_tempMin(data.tempMin);
	config_set_tempMax(data.tempMax);
	config_set_humiMin(data.humiMin);
    config_set_humiMax(data.humiMax);
	config_set_sunshine(data.sunshine);
	config_set_smoke(data.smoke);

}
//从芯片中加载数据
void loudTHData(THData *data)
{
    data->state = config_get_state();
	data->tempMin = config_get_tempMin();
	data->tempMax = config_get_tempMax();
	data->humiMin = config_get_humiMin();
	data->humiMax = config_get_humiMax();
	data->sunshine = config_get_sunshine();
	data->smoke = config_get_smoke();
}
int configNetwork(char* data)
{

    return 1;
}
void 	loadTHData(THData* data)
{

}
int config_eepprom_init()
{
    AT24CXX_Init();
    int times=0;
    while (AT24CXX_Check())
    {
        //printf("AT24C02检测不正常!\r\n");
        times++;
        if (times >10)
        {
            return 0;
        }
        
        delay_ms(20);
    }
    return 1;
}

void config_set_state(u8 state){
	//写数据
	AT24CXX_WriteOneByte(0,state);
}
void config_set_tempMin(u8 temp){
	//写数据
	AT24CXX_WriteOneByte(1,temp);
}
void config_set_tempMax(u8 temp){
	
	//写数据
	AT24CXX_WriteOneByte(2,temp);
}
void config_set_humiMin(u8 humi){
	//写数据
	AT24CXX_WriteOneByte(3,humi);
}
void config_set_humiMax(u8 humi){
	//写数据
	AT24CXX_WriteOneByte(4,humi);
}
void config_set_sunshine(u8 light){
	//写数据
	AT24CXX_WriteOneByte(5,light);
}
void config_set_smoke(u8 smoke){
	//写数据
	AT24CXX_WriteOneByte(6,smoke);
}

u8 config_get_state(void){
	return AT24CXX_ReadOneByte(0);
}
u8 config_get_tempMin(void){
	return AT24CXX_ReadOneByte(1);
}
u8 config_get_tempMax(void){
	return AT24CXX_ReadOneByte(2);
}
u8 config_get_humiMin(void){
	return AT24CXX_ReadOneByte(3);
}

u8 config_get_humiMax(void){
	return AT24CXX_ReadOneByte(4);
}
u8 config_get_sunshine(void){
	return AT24CXX_ReadOneByte(5);
}
u8 config_get_smoke(void){
	return AT24CXX_ReadOneByte(6);
}